//
//  SlpMapKnowledgeItemView.m
//  slp_ios_student
//
//  Created by wyl on 16/9/21.
//  Copyright © 2016年 ND. All rights reserved.
//


#import "SearchViewItemView.h"
#import <Foundation/Foundation.h>

#import "UIView+Extension.h"
#import "UIColor+Extension.h"
#import "BSCommon.h"
#define BUTTON_HEIGHT 30
#define BUTTON_PADDING 40
@interface SearchViewItemView ()

@end

@implementation SearchViewItemView

 
-(UIView *)creatBtn:(NSArray*)arr{
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-30, 500)];
    view.backgroundColor = [UIColor clearColor];
    view.clipsToBounds=YES;
    
    int width = 0;
    int height = 0;
    int number = 0;
    int han = 0;
    
    NSArray *titleArr = arr ;
    CGFloat fontSize = 15;
    int beyondHeight = 0 ;
    //创建button
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 300 + i;
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        button.backgroundColor = [UIColor orangeColor];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 10  ;
        button.layer.masksToBounds = YES ;
        button.layer.borderColor = [[UIColor colorWithHexString:COLOR_LIGHT_GRAY]CGColor];
   
        NSString *character = titleArr[i] ;
        CGSize titleSize = [character boundingRectWithSize:CGSizeMake(999, 25) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
        
        titleSize.width += 20;
        
        float heightTitle = [self heightForString:character fontSize:fontSize andWidth:[UIScreen mainScreen].bounds.size.width-30] + 20;
        
        //自动的折行
        han = han +titleSize.width+30;
        
        if (han > view.width) {
            han = 0;
            han = han + titleSize.width;
            height++;
            width = 0;
            width = width+titleSize.width;
            number = 0;
            if (titleSize.width+10 > view.width) {
                if (i == 0) {
                    height -- ;
                }
                button.frame = CGRectMake(15, BUTTON_PADDING*height+beyondHeight, view.width-15, heightTitle);
                beyondHeight = beyondHeight + (heightTitle - BUTTON_PADDING) + 10;
            }else {
                button.frame = CGRectMake(15, BUTTON_PADDING*height + beyondHeight, titleSize.width+10, BUTTON_HEIGHT);
            }
            
        }else{
            button.frame = CGRectMake(width+15+(number*15), BUTTON_PADDING*height+ beyondHeight, titleSize.width+10, BUTTON_HEIGHT);
            width = width+titleSize.width;
            
        }
        
        self.showMaxY = BUTTON_PADDING*(height+1)+ beyondHeight + 10;
        number++;
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        button.titleLabel.numberOfLines = 0 ;
        [button setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
        [button setTitle:character forState:UIControlStateNormal];
        [view addSubview:button];
    }
    
    return view;
}



//获得字符串的高度
-(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
{
    CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return sizeToFit.height;
}

@end
